<?php
class isp_pembelian_produk_model  extends MY_Model {
	var $model = 'isp_pembelian_produk_model';

	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.id_pembelian,
							a.jumlah,
							a.total_bayar,
							b.kode_customer,
							b.nama,
							b.nama_perusahaan,
							c.kode_inventori,
							c.nama_inventori,
							c.banyak ,
							c.harga 
					FROM	isp_pembelian_produk a 
					LEFT JOIN isp_customer b ON a.kode_customer = b.kode_customer
					LEFT JOIN isp_inventori c ON a.kode_produk = c.kode_inventori
					WHERE	(UPPER(a.id_pembelian) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(c.kode_inventori) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_pembelian' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}


	function GetJSON2()
	{
		$ls_sql	= "	SELECT	*
					FROM	isp_inventori
					WHERE	(UPPER(kode_inventori) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_inventori) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(banyak) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_inventori' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetJSON3()
	{
		$ls_sql	= "	SELECT	*
					FROM	isp_customer
					WHERE	(UPPER(kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_perusahaan) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_customer' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert('isp_pembelian_produk', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_pembelian_produk', $arr_data, $arr_rule);
		//echo $this->db->last_query();
	}

	function UpdateStock(){
		$this->db->update('isp_inventori');
		//echo $this->db->last_query();
	}
	
	function GetSingle($as_id_pembelian){
		$result = $this->db->get_where('isp_pembelian_produk', array('id_pembelian' => $as_id_pembelian));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_pembelian)
	{
		$result = $this->db->get_where('isp_pembelian_produk', array('id_pembelian' => $as_id_pembelian));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_pembelian_produk WHERE id_pembelian= '".$arr_rule['id_pembelian']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_pembelian']."]<br />";
			}else{
				$this->db->delete('isp_pembelian_produk', array('id_pembelian' => $arr_rule['id_pembelian']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>